package com.learn.xueer.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.learn.xueer.R
import com.learn.xueer.app.appViewModel
import com.learn.xueer.app.base.BaseFragment1
import com.learn.xueer.app.util.CacheUtil
import com.learn.xueer.databinding.FragmentPkUserinfoBinding
import com.learn.xueer.ui.viewmodel.PkUserInfoViewModel
import kotlinx.android.synthetic.main.fragment_pk_userinfo.btnNext
import kotlinx.android.synthetic.main.fragment_pk_userinfo.iv_avatar
import kotlinx.android.synthetic.main.fragment_pk_userinfo.tv_count
import kotlinx.android.synthetic.main.fragment_pk_userinfo.tv_grade
import kotlinx.android.synthetic.main.fragment_pk_userinfo.tv_name
import kotlinx.android.synthetic.main.fragment_pk_userinfo.tv_rate
import kotlinx.android.synthetic.main.fragment_pk_userinfo.tv_yes_count

class PkUserInfoFragment : BaseFragment1<PkUserInfoViewModel, FragmentPkUserinfoBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        addLoadingObserve(mViewModel)
        mViewModel.getPkRecord()
        btnNext.setOnClickListener {
            findNavController().navigate(R.id.navToSearch)
        }
        CacheUtil.getUser()?.let {
            Glide.with(iv_avatar)
                .load(it.avatar)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(iv_avatar)
            tv_name.text = it.nickname
            tv_grade.text = "${it.school?.name} ${it.grade?.gradeName}"
        }
    }

    @SuppressLint("SetTextI18n")
    override fun createObserver() {
        super.createObserver()
        mViewModel.pkRecordData.observe(this, Observer {
            tv_count.text = "${it.count}"
            tv_yes_count.text = "${it.yesCount}"
            tv_rate.text = "${it.rate.toInt()}%"
        })
    }

}